package main;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

public class Parser {

	public static void main(String[] args) {
		
		// Using HashMap because sorting isn't important. This will store an artist as a key and a list of what lines it appeared on as a value;
		HashMap<String,ArrayList<Integer>> artistAppearances = new HashMap<String,ArrayList<Integer>>();
		
		try {
			BufferedReader reader = new BufferedReader(new FileReader("bin/main/Artist_lists_small.txt"));
			
			String line;
			Integer lineNum = new Integer(0);
			
			// Read in the file and build the list of artists and what lines they appear on
			try {
				while((line = reader.readLine()) != null)
				{
					String[] artists = line.split(",");
					
					for(int i = 0; i < artists.length; i++)
					{
						if(artistAppearances.containsKey(artists[i]) == false)
						{
							ArrayList<Integer> appearances = new ArrayList<Integer>();
							appearances.add(lineNum);
							artistAppearances.put(artists[i], appearances);
						}
						else
						{
							artistAppearances.get(artists[i]).add(lineNum);
						}
					}
					
					lineNum++;
				}
			} 
			finally
			{
				reader.close();
			}
			
		} catch (IOException e) {
			
			e.printStackTrace();
		}
		
		// If an artist appears < 50 times then it can't be in 50+ pairs so delete it.
		Iterator<Entry<String, ArrayList<Integer>>> iter = artistAppearances.entrySet().iterator();
		while(iter.hasNext())
		{
			Map.Entry<String, ArrayList<Integer>> entry = iter.next();
		    ArrayList<Integer> value = entry.getValue();
		    
		    if(value.size() < 50)
		    {
		    	iter.remove();
		    }
		    
		}
		
		// Using a set to avoid duplicate entries (i.e. Kasbian, Muse & Muse, Kasabian)
		HashSet<String> pairs = new HashSet<String>();
		
		// Using probability, we could probably avoid having to do a nested loop like this but this was the best way I could think of
		Iterator<Entry<String, ArrayList<Integer>>> i = artistAppearances.entrySet().iterator();
		while(i.hasNext())
		{
			Map.Entry<String, ArrayList<Integer>> entry = i.next();
		    ArrayList<Integer> value = entry.getValue();
		    String key = entry.getKey();

		    Iterator<Entry<String, ArrayList<Integer>>> j = artistAppearances.entrySet().iterator();
			while(j.hasNext())
			{
				Map.Entry<String, ArrayList<Integer>> entry2 = j.next();
			    ArrayList<Integer> value2 = entry2.getValue();
			    String key2 = entry2.getKey();
			    
			    // Don't do anything if they're the same artist
			    if(key.equals(key2))
			    {
			    	continue;
			    }
			    else // Otherwise find the intersection of the two appearance lists. Print if >= 50
			    {
			    	ArrayList<Integer> intersection = new ArrayList<Integer>(value2);
			    	intersection.retainAll(value);
			    	
			    	if(intersection.size() >= 50)
			    	{
			    		// Just printing out the intersection means that we can encounter duplicates
			    		// The problem said we cannot store ALL possible pairs, but I interpreted that to mean that we can store pairs with >= 50 appearances
			    		//System.out.println(key + ", " + key2);
			    		
			    		// Order the pair alphabetically and add it. We do the ordering to filter out duplicates like the Kasabian, Muse case
			    		if(key.compareTo(key2) < 0)
			    		{
			    			pairs.add(key + ", " + key2 + "\n");
			    		}
			    		else
			    		{
			    			pairs.add(key2 + ", " + key + "\n");
			    		}
			    	}
			    }
			}   
		}

		// Print out the final list of pairs
		Iterator<String> pairIter = pairs.iterator();
		while(pairIter.hasNext())
		{
			System.out.print(pairIter.next());
		}
	}
}
